/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package paquetevistas.almacen;

import Seguridad.Controlador.controladorseguridad;
import controlador.almacen.controladoralmacen;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.swing.JOptionPane;
import javax.swing.table.AbstractTableModel;
import modelos.Ordenproduccion;
import modelos.Producto;
import modelos.Requirimientocompra;
import modelos.Usuario;
import paquetevistas.Escritorio;

/**
 *
 * @author Karina
 */
public class NecesidadReal extends javax.swing.JInternalFrame {

    /**
     * Creates new form NecesidadReal
     */
    
    List<modelos.Productoxreceta> orden2 = lbj.LBJ.Login.escritorio.necesidadestimada.orden;
    controladoralmacen mycontroladoralmacen = new controladoralmacen();
    public static List<Double> cantidadesplan=lbj.LBJ.Login.escritorio.necesidadestimada.cantidadesplan;
    int idordeninicial= lbj.LBJ.Login.escritorio.stock.idorden;

    OrdenCompraTableModel ordenreal = new OrdenCompraTableModel();
    
    //Para el registro de Log
    public static Usuario usuario = lbj.LBJ.Login.usuario;
    public static controladorseguridad mycontroladorseguridad = new controladorseguridad();
    
    public NecesidadReal() {
        initComponents();
        jTable1.setModel(ordenreal);
    }
    
   
    
    class OrdenCompraTableModel extends AbstractTableModel{

        public OrdenCompraTableModel(){
            //FALTA BUSCAR ORDEN
            
            mycontroladoralmacen.mySort(orden2);
            mycontroladoralmacen.acumulate(orden2);
            
        

        }
        
        String[] titles = {"Nombre", "Cantidad", "Unidad"};
        
        @Override
        public int getRowCount() {
             return orden2.size();
        }

        @Override
        public int getColumnCount() {
            return 3;
        }

        @Override
        public Object getValueAt(int rowIndex, int columnIndex) {
            String res=null;
            modelos.Productoxreceta p = orden2.get(rowIndex);
            switch (columnIndex){
                case 0: res = "" + p.getProducto().getNombre();
                        break;
                case 1: 
                        if(p.getCantidad()< p.getProducto().getStock())
                        res = "" + 0;
                        else res = "" + (p.getCantidad()-p.getProducto().getStock());
                        break;
                case 2: res = "" + p.getProducto().getUnidadmedida().getNombre();
                        break; 
            }
            return res;
        }
        
        public String getColumnName(int col){
            return titles[col];
        } 

}

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel3 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jLabel1 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();

        jLabel3.setText("Galletas Rellenita");

        setClosable(true);
        setIconifiable(true);
        setMaximizable(true);
        setTitle("Necesidad Real de Materias Primas");

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Nombre", "Tipo", "Cantidad", "Unidad"
            }
        ));
        jScrollPane1.setViewportView(jTable1);

        jLabel1.setText("Orden de Compra");

        jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenes/file plus.png"))); // NOI18N
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jLabel2.setText("Generar Orden de Compra");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 54, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(55, 55, 55))
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addComponent(jLabel2)
                                .addGap(23, 23, 23))
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 482, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addContainerGap())))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(24, 24, 24)
                .addComponent(jLabel1)
                .addGap(27, 27, 27)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 199, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jButton1)
                .addGap(8, 8, 8)
                .addComponent(jLabel2)
                .addContainerGap(17, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        mycontroladoralmacen.guardaOrdenCompra();
        int i = mycontroladoralmacen.guardaDetalleOrdenCompra(orden2);
        Ordenproduccion o = mycontroladoralmacen.sacoOrdenxid(idordeninicial);
        mycontroladoralmacen.editaEstadoOrden(o, 1);
        if (i==0) {
            JOptionPane.showMessageDialog(this, "Se generó el Requerimiento de Compra correctamente");
            this.dispose();
            
            mycontroladorseguridad.insertarlog(usuario, new Date(), "Insertar", "Requerimientocompraxproducto", "Insertar Requerimientocompraxproducto");
        }
        else JOptionPane.showMessageDialog(this, "Hubo un error al generarse el Requerimiento");
        ordenreal=null;
    }//GEN-LAST:event_jButton1ActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    // End of variables declaration//GEN-END:variables

    
}
